package com.huhao.wshare.Friends;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.huhao.wshare.R;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class PendingFriendRequestAdapter extends RecyclerView.Adapter<PendingFriendRequestAdapter.ViewHolder> {

    private List<JSONObject> pendingRequests = new ArrayList<>();
    private OnRequestActionListener listener;

    public interface OnRequestActionListener {
        void onAcceptRequest(int requestId);
        void onRejectRequest(int requestId);
    }

    public void setOnRequestActionListener(OnRequestActionListener listener) {
        this.listener = listener;
    }

    public void setPendingRequests(List<JSONObject> requests) {
        this.pendingRequests = requests;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_pending_friend_request, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        JSONObject request = pendingRequests.get(position);
        holder.tvAccount.setText(request.optString("account"));
        holder.tvUsername.setText(request.optString("username"));

        // 处理头像
        String avatar = request.optString("avatar");
        String gender = request.optString("gender");
        int defaultAvatarResId;
        if ("man".equals(gender)) {
            defaultAvatarResId = R.drawable.default_avatar_man;
        } else if ("woman".equals(gender)) {
            defaultAvatarResId = R.drawable.default_avatar_woman;
        } else {
            defaultAvatarResId = R.drawable.default_avatar_man;
        }

        if (avatar != null && !avatar.isEmpty()) {
            Glide.with(holder.itemView.getContext())
                    .load(avatar)
                    .apply(RequestOptions.circleCropTransform())
                    .placeholder(defaultAvatarResId)
                    .error(defaultAvatarResId)
                    .into(holder.ivAvatar);
        } else {
            holder.ivAvatar.setImageResource(defaultAvatarResId);
        }

        // 处理性别图标 (保持不变)
        int drawableResId = 0;
        if ("man".equals(gender)) {
            drawableResId = R.drawable.ic_male;
        } else if ("woman".equals(gender)) {
            drawableResId = R.drawable.ic_female;
        }
        if (drawableResId != 0) {
            holder.ivGender.setImageDrawable(ContextCompat.getDrawable(holder.itemView.getContext(), drawableResId));
            holder.ivGender.setVisibility(View.VISIBLE);
        } else {
            holder.ivGender.setVisibility(View.GONE);
        }

        String type = request.optString("type");
        if ("SENT".equals(type)) {
            // 发出的请求：显示"等待验证"文本，隐藏按钮
            holder.tvPending.setVisibility(View.VISIBLE);
            holder.ivAccept.setVisibility(View.GONE);
            holder.ivReject.setVisibility(View.GONE);
        } else if ("RECEIVED".equals(type)) {
            // 收到的请求：显示接受/拒绝按钮，隐藏文本
            holder.tvPending.setVisibility(View.GONE);
            holder.ivAccept.setVisibility(View.VISIBLE);
            holder.ivReject.setVisibility(View.VISIBLE);
            int requestId = request.optInt("requestId");

            holder.ivAccept.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onAcceptRequest(requestId);
                }
            });

            holder.ivReject.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onRejectRequest(requestId);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return pendingRequests.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvAccount;
        TextView tvUsername;
        ImageView ivAvatar;
        ImageView ivGender;
        TextView tvPending;
        ImageView ivAccept;
        ImageView ivReject;

        ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvAccount = itemView.findViewById(R.id.tv_account);
            tvUsername = itemView.findViewById(R.id.tv_username);
            ivAvatar = itemView.findViewById(R.id.iv_avatar);
            ivGender = itemView.findViewById(R.id.iv_gender);
            tvPending = itemView.findViewById(R.id.tv_pending);
            ivAccept = itemView.findViewById(R.id.iv_accept);
            ivReject = itemView.findViewById(R.id.iv_reject);
        }
    }
}